﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mighty.Common;
using Mighty.Brain;
using System.Collections;

namespace Mighty.Brain
{
    public class OnepieceBrain : BasicBrain
    {
        //조커는 arraycard[0,0]에 저장한다.
        public int[,] arraycard = new int[5, 15];

        int max = 0; //내 카드 중 가장 많은 무늬의 수
        int myshape = 0; //내 카드 주 가장 많은 무늬
        int scorecard = 0; //myshape 중 scorecard의 수
           

       public override Betting BettingTicEvent()
        {
            int i, j;

           
            
           
            ///////////////////////////////////////////////////////////
            ////////게임시작 전에 카드를 정렬시킨다////////////////////
            ///////////////////////////////////////////////////////////
            for (i = 0; i < 10; i++)
            {               
                HandedCard n = gameInfo.myCards[i];
                
                int s = (int)n.shape;
                arraycard[s, n.number] = 1;
            }


            ///////////////////////////////////////////////////////////
            /////////배팅 전에 shape & joker 분석//////////////////////
            ///////////////////////////////////////////////////////////
            for (i = 0; i < 5; i++)
            {
                int nowmax = 0;
                for (j = 0; j < 15; j++)
                {
                    if (arraycard[i, j] == 1)
                        nowmax++;
                }

                if (max < nowmax)
                {
                    max = nowmax;
                    myshape = i;
                }

            }

            ///////////////////////////////////////////////////////////
            ////////scorecard의 수/////////////////////////////////////
            ///////////////////////////////////////////////////////////
            for (i = 10; i < 15; i++)
            {
                if (arraycard[myshape, i] == 1)
                    scorecard++;
            }


            ///////////////////////////////////////////////////////////
            //////////////배팅시작/////////////////////////////////////
            ///////////////////////////////////////////////////////////
            Betting betting = new Betting();

            betting.isPass = true;

            //누군가가 먼저 배팅했을때
            if (gameInfo.betting.limit > 0)
            {
                Betting previousBetting = gameInfo.betting;

                if (max == 5 && scorecard == 3)
                {
                    if (previousBetting.limit < 15)
                    {
                        betting.isPass = false;
                        betting.limit = 15;
                        betting.giruda = (Giruda)myshape;
                    }
                }

                else if (max == 6 && scorecard == 3)
                {
                    if (previousBetting.limit < 16)
                    {
                        betting.isPass = false;
                        betting.limit = 16;
                        betting.giruda = (Giruda)myshape;
                    }
                }

                else if (max == 5 && scorecard == 2)
                {
                    if (previousBetting.limit < 16)
                    {
                        betting.isPass = false;
                        betting.limit = 14;
                        betting.giruda = (Giruda)myshape;
                    }
                }

                else if (max >= 6 && scorecard >= 4)
                {
                    if (previousBetting.limit < 17)
                    {
                        betting.isPass = false;
                        betting.limit = 17;
                        betting.giruda = (Giruda)myshape;
                    }
                }

                else
                {
                    betting.isPass = true;
                }
                
            }

            //내가 처음 배팅할때
            else
            {
                betting.isPass = false;
                
                if (max == 5 && scorecard==3)
                {
                    betting.limit = 15;
                    betting.giruda = (Giruda)myshape;
                }

                else if (max == 5 && scorecard == 2)
                {
                    betting.limit = 14;
                    betting.giruda = (Giruda)myshape;
                }

                else if(max == 6 && scorecard == 3)
                {
                    betting.limit = 16;
                    betting.giruda = (Giruda)myshape;
                }

                else if (max >= 6 && scorecard >= 4)
                {
                    betting.limit = 17;
                    betting.giruda = (Giruda)myshape;
                }

                else
                {
                    betting.isPass = true;
                }

            }
            return betting;

        }

       public override void BettingCompleteEvent()
       {
       }
        
       ///////////////////////////////////////////////////////////
       //////////////주공일때 카드 선택///////////////////////////
       ///////////////////////////////////////////////////////////
       public override HandedCard[] ChoiceCardEvent()
       {
           int i,j;
           int count = 0;

           //새로 받은 카드를 arraycard에 저장한다.
           for (i = 10; i <= 12; i++)
            {
                HandedCard n = gameInfo.myCards[i];

                int s = (int)n.shape;
                arraycard[s, n.number] = 1;
            }

           while (count < 3)
           {
               for (j = 10; j < 14; j++)
               {
                   for (i = 0; i < 5; i++)
                   {
                       if (arraycard[i, j] == 1 && i != myshape && count < 4)
                       {
                           arraycard[i, j] = 0;
                           count++;
                       }

                   }

               }

               for (j = 2 ; j < 10; j++)
               {
                   for (i = 0; i < 5; i++)
                   {
                       if (arraycard[i, j] == 1 && i != myshape && count < 4)
                       {
                           arraycard[i, j] = 0;
                           count++;
                       }

                   }

               }

           }

           //손에 있는 3장의 카드를 버린다.
           for (int k = 0; k < 13; k++)
           {
               HandedCard n = gameInfo.myCards[k];

               for (i = 0; i < 15; i++)
               {
                   for (j = 0; j < 5; j++)
                   {
                       if ((int)n.shape == j && n.number == i && arraycard[j, i] == 0)
                       {
                           gameInfo.myCards[k].cmdRemove = true;
                           break;
                       }
                       else
                           gameInfo.myCards[k].cmdRemove = false;
                   }
               }
           }

               return gameInfo.myCards;
       }

       ///////////////////////////////////////////////////////////
       //////////////주공일때 프랜드 선택/////////////////////////
       ///////////////////////////////////////////////////////////

       public override FriendPlayer ChoiceFriendEvent()
       {
           if ((int)gameInfo.betting.giruda != 1)
           {
               if (arraycard[2, 14] == 0)
               {
                   gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.card;
                   gameInfo.friend.cardCondition = gameInfo.mighty;
               }
               else if (arraycard[4, 0] == 0)
               {
                   gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.card;
                   gameInfo.friend.cardCondition = new Card(Shape.Joker, 0);
               }
               else
                   gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.firstwin;
           }
           else
           {
               if (arraycard[1, 14] == 0)
               {
                   gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.card;
                   gameInfo.friend.cardCondition = gameInfo.mighty;
               }
               else if (arraycard[4, 0] == 0)
               {
                   gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.card;
                   gameInfo.friend.cardCondition = new Card(Shape.Joker, 0);
               }
               else
                   gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.firstwin;

           }

           return gameInfo.friend;
       }

       ///////////////////////////////////////////////////////////
       //////////////매턴 공격, 수비 패턴/////////////////////////
       ///////////////////////////////////////////////////////////
       public override HandedCard RoundTicEvent()
       {
           int i =0;
           HandedCard h = new HandedCard();
           if (gameInfo.attackPlayer.name == gameInfo.myInfo.name)
           {

               if (gameInfo.roundIdx == 0)
               {
                   for (i = 0; i < 4; i++)
                   {
                       if (arraycard[i, 14] == 1 && (int)gameInfo.mighty.shape != i)
                       {
                           h.shape = (Shape)i;
                           h.number = 14;
                           return h;
                       }
                   }

                   int j = 0;
                   HandedCard tmp_card = gameInfo.myCards[j];

                   h.number = 20;
                   for (j = 0; j < 10; j++)
                   {

                       if ((int)tmp_card.shape != (int)gameInfo.betting.giruda && h.number > tmp_card.number)
                       {
                           h.shape = tmp_card.shape;
                           h.number = tmp_card.number;
                       }
                   }

                   return h;

               }

           }
           return h;
        /*
           ArrayList possibleCards = new ArrayList();
           foreach (HandedCard card in gameInfo.myCards)
           {
               if (card.isPossible) possibleCards.Add(card);
           }

           Random r = new Random();
           int k = r.Next(possibleCards.Count);

           return (HandedCard)possibleCards[k];
         */
       }
        
       
    }
}
